<?php

namespace App\Service\Check\CmbBank;

use App\Exceptions\Custom\CustomException;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

class CloudDirectConnectClient
{
    public static function accountFlow(string $date, int $page, string $subBankCode, string $accountNum, string $api_url, string $bankPubKey, string $privateKey, string $sm4key, string $uid): array
    {
        if ($page < 1) {
            throw new CustomException('错误的招商银行银企直连页码参数');
        }
        $json   = json_encode([
            'accnbr'     => $accountNum,
            'bbknbr'     => $subBankCode,
            'funcode'    => 'DCTRSINF',
            'trsdat'     => str_replace('-', '', $date),
            'url'        => $api_url,
            'bankpubkey' => $bankPubKey,
            'privkey'    => $privateKey,
            'sm4key'     => $sm4key,
            'uid'        => $uid,
            'trsseq'     => strval(200 * ($page - 1)),
        ]);
        $base64 = base64_encode($json);
        $client = new Client([
            RequestOptions::HTTP_ERRORS => 'false',
        ]);
        $data1  = $client->post(config('check.cmb.proxy') . '/sm/process', [
            'form_params' => [
                'base64_str' => $base64,
            ],
        ])->getBody()->getContents();
        $data1  = json_decode($data1, true);
        if (!isset($data1['data']) || !is_array($data1['data'])) {
            return [false, $data1['Message'] ?? '没有返回密文'];
        }
        return [true, $data1];
    }
}
